package com.acme.bankapp.domain.account;
import com.acme.exceptions.*;

@SuppressWarnings("serial")
public class CheckingAccount extends AbstractAccount {

	private double overdraft;
	
	public CheckingAccount(double amount, double overdraft) {
		super(amount);
		this.accountInfo.append("Account initiatial amount " + amount + "\n");
		this.overdraft = ((overdraft > 0) ? overdraft : 0);
		this.accountInfo.append("Overdraft amount " + overdraft + "\n");
	}
	
	@Override
	public void withdraw (double amount) throws NotEnoughFundsException {
		if (amount >= 0) {
			if (this.balance + this.overdraft >= amount) {
				this.balance -= amount;	
				this.accountInfo.append("Withdrawed: " + amount + "\n");
			} else {
				throw new OverDraftLimitExceedException("Not enough funds!",this);
			}
		} else{
			throw new IllegalArgumentException("amount should be non-negative!");
		}
		
	}

	@Override
	public double maximumAmountToWithdraw() {
		return (this.balance + this.overdraft);
	}

	@Override
	public String getAccountType(Account account) {
		return "CheckingAccount";
	}

	@Override
	public boolean equals(Object obj) {
		if(this == obj) return true;
		if(obj == null) return false;
		final CheckingAccount other = (CheckingAccount)obj;
		if(this.balance != other.balance) return false;
		if(this.id != other.id) return false;
		if(this.overdraft != other.overdraft) return false;
		if(!this.accountInfo.toString().equals(other.accountInfo.toString())) return false;
		return true;
	}

	@Override
	public int hashCode() {
		int hash = 37;
		hash = hash + 23 * this.accountInfo.hashCode();
		hash = hash + 17 * (int)this.balance;
		hash = hash + 13 * (int)this.overdraft;
		hash = hash + 29 * this.id;
		hash = Math.abs(hash % 89);
		return hash;
	}

	@Override
	public int toDecimalValue() {
		return (int)Math.round(this.maximumAmountToWithdraw());
	}

}
